import 'package:flutter/material.dart';

import 'InputWidget.dart';
import 'CardItem.dart';
import 'package:my_flutter_github/MyStyle.dart';

/**
 * issue 编辑输入框
 * Created by guoshuyu
 * on 2018/7/21.
 */
class IssueEditDialog extends StatefulWidget {
  final String dialogTitle;

  final ValueChanged<String> onTitleChanged;

  final ValueChanged<String> onContentChanged;

  final VoidCallback onPressed;

  final TextEditingController titleController;

  final TextEditingController valueController;

  final bool needTitle;

  const IssueEditDialog(
      {Key key,
      this.dialogTitle,
      this.onTitleChanged,
      this.onContentChanged,
      this.onPressed,
      this.titleController,
      this.valueController,
      this.needTitle = true})
      : super(key: key);

  @override
  _IssueEditDialogState createState() => _IssueEditDialogState();
}

class _IssueEditDialogState extends State<IssueEditDialog> {
  _createTitleInput() {
    return (widget.needTitle)
        ? new Padding(
            padding: new EdgeInsets.all(4.0),
            child: new InputWidget(
              onChanged: widget.onTitleChanged,
              controller: widget.titleController,
              hintText: "请输入标题",
              obscureText: false,
            ),
          )
        : new Container();
  }

  //快速输入框
  _createFastInputContainer() {
    ///因为是Column下包含了ListView，所以需要设置高度
    return new Container(
      height: 30.0,
      child: ListView.builder(
          scrollDirection: Axis.horizontal,
          itemCount: FAST_INPUT_LIST.length,
          itemBuilder: (context, index) {
            return new RawMaterialButton(
              materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
              padding: new EdgeInsets.only(left: 8.0, top: 8.0, right: 5.0, bottom: 5.0),
              constraints: new BoxConstraints(minWidth: 0.0, minHeight: 0.0),
              child: new Icon(
                FAST_INPUT_LIST[index].iconData,
                size: 16.0,
              ),
              onPressed: () {
                String text = FAST_INPUT_LIST[index].content;
                String newText = "";
                if (widget.valueController.value != null) {
                  newText = widget.valueController.value.text;
                }
                newText = newText + text;
                setState(() {
                  widget.valueController.value =
                  new TextEditingValue(text: newText);
                });
                widget.onContentChanged?.call(newText);
              },
            );
          }),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: MediaQuery.of(context).size.height,
      width: MediaQuery.of(context).size.width,
      ///Dialog 背景颜色
      color: Colors.black12,

      ///触摸收起键盘
      child: new GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          FocusScope.of(context).requestFocus(new FocusNode());
        },
        child: new Center(
          child: new CardItem(
            margin: new EdgeInsets.all(20.0),
            shape: new RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(10.0))),
            child: new Padding(
              padding: new EdgeInsets.all(12.0),
              child: new Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  ///dialog 标题
                  new Padding(
                    padding: new EdgeInsets.only(top: 5.0, bottom: 10.0),
                    child: new Center(
                      child: new Text(
                        widget.dialogTitle,
                        style: MyConstants.normalTextBold,
                      ),
                    ),
                  ),

                  ///issue标题输入框
                  _createTitleInput(),

//                  ///内容输入框
                  new Container(
                    height: MediaQuery.of(context).size.width * 3 / 4,
                    //给issue内容输入框加一个背景边框
                    decoration: new BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(4.0)),
                      color: Colors.white,
                      border: Border.all(
                          color: Color(MyColors.subTextColor), width: .3),
                    ),
//                    padding: new EdgeInsets.only(left: 20.0, top: 12.0, right: 20.0, bottom: 12.0),
                  padding: EdgeInsets.all(12.0),
                    child: new Column(
                      children: <Widget>[
                        new Expanded(
                            child: new TextField(
                          autofocus: false,
                          maxLines: 999,
                          onChanged: widget.onContentChanged,
                          controller: widget.valueController,
                          decoration: new InputDecoration.collapsed(
                              hintText: "请输入文字内容",
                              hintStyle: MyConstants.middleSubText),
                          style: MyConstants.middleText,
                        )),

                        //创建快速输入框
                        _createFastInputContainer(),
                      ],
                    ),
                  ),

                  new Container(
                    height: 10.0,
                  ),

                  //buttons
                  new Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      //取消
                      new Expanded(
                          child: new RawMaterialButton(
                              materialTapTargetSize:
                                  MaterialTapTargetSize.shrinkWrap,
                              padding: EdgeInsets.all(10.0),
                              constraints: new BoxConstraints(
                                  minWidth: 0.0, minHeight: 0.0),
                              child: new Text(
                                "取消",
                                style: MyConstants.normalSubText,
                              ),
                              onPressed: () {
                                Navigator.pop(context);
                              })),

                      new Container(
                          width: 0.3,
                          height: 25.0,
                          color: Color(MyColors.subTextColor)),

                      //确定
                      new Expanded(
                          child: new RawMaterialButton(
                              materialTapTargetSize:
                                  MaterialTapTargetSize.shrinkWrap,
                              padding: EdgeInsets.all(10.0),
                              constraints: new BoxConstraints(
                                  minWidth: 0.0, minHeight: 0.0),
                              child: new Text(
                                "确定",
                                style: MyConstants.normalTextBold,
                              ),
                              onPressed: widget.onPressed)),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

var FAST_INPUT_LIST = [
  FastInputIconModel(MyIcons.ISSUE_EDIT_H1, "\n# "),
  FastInputIconModel(MyIcons.ISSUE_EDIT_H2, "\n## "),
  FastInputIconModel(MyIcons.ISSUE_EDIT_H3, "\n### "),
  FastInputIconModel(MyIcons.ISSUE_EDIT_BOLD, "****"),
  FastInputIconModel(MyIcons.ISSUE_EDIT_ITALIC, "__"),
  FastInputIconModel(MyIcons.ISSUE_EDIT_QUOTE, "` `"),
  FastInputIconModel(MyIcons.ISSUE_EDIT_CODE, " \n``` \n\n``` \n"),
  FastInputIconModel(MyIcons.ISSUE_EDIT_LINK, "[](url)"),
];

class FastInputIconModel {
  final IconData iconData;
  final String content;

  FastInputIconModel(this.iconData, this.content);
}
